{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a20a2420",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original polynomial: x**3 - 10*x**2 + 31*x - 30\n",
      "Factorization of polynomial: (x - 5)*(x - 3)*(x - 2)\n"
     ]
    }
   ],
   "source": [
    "# Prime Factorization of polynomials\n",
    "from sympy import symbols, factor\n",
    "from sympy.abc import x\n",
    "\n",
    "polynomial = x**3 -10*x**2 + 31*x - 30\n",
    "\n",
    "factored_polynomial = factor(polynomial)\n",
    "\n",
    "print(\"Original polynomial:\", polynomial)\n",
    "print(\"Factorization of polynomial:\", factored_polynomial)\n",
    "\n",
    "# Output\n",
    "# Original polynomial: x**3 - 10*x**2 + 31*x - 30\n",
    "# Factorization of polynomial: (x - 5)*(x - 3)*(x - 2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "89ff714b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lagrange Polynomial: (x - 3)*(x - 2) - 3*(x - 3)*(x - 1) + 4*(x - 2)*(x - 1)\n",
      "Simplified Polynomial: 2*x**2 - 5*x + 5\n",
      "Value at x=4: 8\n"
     ]
    }
   ],
   "source": [
    "# Lagrange Interpolation Method\n",
    "from sympy import symbols\n",
    "from sympy.abc import x\n",
    "from sympy.polys.polyfuncs import interpolating_poly\n",
    "\n",
    "# Given interpolation points and their corresponding function values\n",
    "data = [(1, 2), (2, 3), (3, 8)]\n",
    "\n",
    "if isinstance(data, dict):\n",
    "    X, Y = list(zip(*data.items()))\n",
    "else:\n",
    "    if isinstance(data[0], tuple):\n",
    "        X, Y = list(zip(*data))\n",
    "    else:\n",
    "        X = list(range(1, n + 1))\n",
    "        Y = list(data)\n",
    "\n",
    "# Use the interpolating_poly function to construct the Lagrange interpolation polynomial\n",
    "interpolation_polynomial = interpolating_poly(len(data), x, X, Y)\n",
    "\n",
    "# Output the interpolation polynomial\n",
    "print(\"Lagrange Polynomial:\", interpolation_polynomial)\n",
    "print(\"Simplified Polynomial:\", interpolation_polynomial.expand())\n",
    "# Calculate the value at x=4 using the interpolation polynomial\n",
    "result = interpolation_polynomial.subs(x, 3)\n",
    "print(\"Value at x=4:\", result)\n",
    "\n",
    "# Sample Output\n",
    "# Lagrange Polynomial: (x - 3)*(x - 2) - 3*(x - 3)*(x - 1) + 4*(x - 2)*(x - 1)\n",
    "# Simplified Polynomial: 2*x**2 - 5*x + 5\n",
    "# Value at x=4: 8\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f1b05a6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
